package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.GuadanSlAdapter
import client.comm.baoding.api.bean.GoodsDetailTrade
import client.comm.baoding.databinding.ActivityDealDetailBinding
import client.comm.baoding.dialog.JyInputNumDialog
import client.comm.baoding.ui.vm.TradeDetailModel
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import java.util.*

/**
 * 商品购买 -废弃
 *
 */
class DealDetailActivity : BaseMiddleActivity<TradeDetailModel, ActivityDealDetailBinding>() {

//    lateinit var bannerList: List<GoodsDetail.Banner>
//    lateinit var detailList: List<GoodsDetail.Detail>
    var tGoodsDetail: GoodsDetailTrade? = null
    var goods_id: String? = ""

    override fun getLayout(): Int {
        return R.layout.activity_deal_detail
    }

    override fun initViewModel(): TradeDetailModel = createViewModel()

    override fun initView() {
        binding.vm = viewModel
        binding.event = this
        binding.refreshLayout.setOnRefreshListener {
            getData()
        }
        binding.emptyLayout.setOnClickListener {
            getData()
        }

//        bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")!!
//        detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")!!
        goods_id = intent?.getStringExtra("goods_id")!!
    }

    val guaDanAdapter: GuadanSlAdapter by lazy {
        GuadanSlAdapter(this).apply {
            onZhaiDan = {
                JyInputNumDialog(this@DealDetailActivity, 3).apply {
                    ok = { num ->
                        dismiss()
                    }
                }.show()
            }
        }
    }

    override fun initData() {
        getData()
    }

    override fun observerUI() {
//        viewModel.callUser.observe(this, {
//            it?.let {
//                if (it.isSuccess) {
//                    binding.userInfo = it.data
//                } else {
//                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
//                }
////                binding.refreshLayout.isRefreshing = false
//            }
//        })

//        viewModel.goodsDetail.observe(this, {
//            it?.let {
//                if (it.isSuccess && it.data != null) {
////                    bannerList = it.data!!.banner_list as ArrayList<GoodsDetail.Banner>
////                    detailList = it.data!!.detail_list as ArrayList<GoodsDetail.Detail>
//
//                    if (bannerList != null && bannerList.size > 0) {
//                        val width = getScreenWidth(this)
//                        val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
//                        params.height = width //(width * 575 / 1400f).toInt()
//                        binding.banner.setLayoutParams(params)
//                        binding.banner.setIndicator(CircleIndicator(this))
//                        val list = bannerList
//                        binding.banner.setAdapter(object : SyBannerAdapter<GoodsDetail.Banner>(list) {
//                            override fun onBindView(
//                                holder: BannerViewHolder?,
//                                data: GoodsDetail.Banner?,
//                                position: Int,
//                                size: Int
//                            ) {
//                                mImgLoader.load(data?.goods_img)
//                                    .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
//                                        holder!!.imageView
//                                    )
//                            }
//                        })
//                        binding.banner.start()
//                    }
//
//                    it.message?.let {
//                        toast(it)
//                    }
//
//                }
//            }
//        })
//        viewModel.goodsDetail.observe(this, {
//            it?.let {
//                if (it.isSuccess && it.data != null) {
//                    binding.emptyLayout.visibility = View.GONE
////                    binding.bean = it.data
//
////                    viewModel.user_status.value = it.data!!.user_status
////                    var count = Math.max(
////                        it.data!!.f_hanging_order_list.size,
////                        it.data!!.g_hanging_order_list.size
////                    )
////                    guaDanAdapter.submitList(als)
//                    it.message?.let {
//                        toast(it)
//                    }
//
//                } else {
//                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
//                }
//                binding.refreshLayout.isRefreshing = false
//            }
//        })

        viewModel.tradeGoodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    binding.emptyLayout.visibility = View.GONE
                    binding.bean = it.data
                    tGoodsDetail = it.data

                    if (tGoodsDetail?.order_list.isNullOrEmpty()) {
                        binding.llGuadansl.visibility = View.GONE
                    } else {
                        binding.llGuadansl.visibility = View.VISIBLE

                        tGoodsDetail?.order_list?.let {
                            binding.rvGuadan.adapter = guaDanAdapter
                            guaDanAdapter.submitList(it)
                        }
                    }
                    it.message?.let {
                        toast(it)
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    getData()
                }
            }
        })
    }

    private fun getData() {
        viewModel.holdGoodsDetailCall(goods_id!!)
//        viewModel.userInfo()
//        viewModel.holdTradeCall(goods_id!!)

    }

    //查看商品详情
    fun goDetail(goodsId: String) {
        startActivity(Intent(this, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", goodsId)
        })
    }


    //当前价购买0 / 特价购买1
    fun goDifTypeBuy(type: Int) {
        when (type) {
            1 -> {//特价购
                tGoodsDetail?.let {
                    JyInputNumDialog(this, 1).apply {
                        ok = { num ->
                            viewModel.holdBuyTj(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }
//            2 -> {//优惠购
//                tGoodsDetail?.let {
//                    JyInputNumDialog(this, 1).apply {
//                        ok = { num ->
//                            viewModel.holdBuyYh(it, num)
//                            dismiss()
//                        }
//                    }.show()
//                }
//            }
            else -> {//当前购
//                tGoodsDetail?.let {
//                    JyInputNumDialog(this, 1).apply {
//                        ok = { num ->
//                            viewModel.holdBuyYj(it, num)
//                            dismiss()
//                        }
//                    }.show()
//                }
            }
        }
        /* startActivity(Intent(this, XyzrNewActivity::class.java).apply {
             putExtra("index", 1)
             putExtra("titleName", "")
         })*/
    }


    fun onTihuo() {
        toast("暂未开放")

//        tGoodsDetail?.let {
//            JyInputNumDialog(this, 2).apply {
//                ok = { num ->
//                    viewModel.holdBuyTh(it, num)
//                    dismiss()
//                }
//            }.show()
//        }
    }

    /**
     * 资产记录
     */
    fun goZichanJl() {
        startActivity(Intent(this, ZcJl2Activity::class.java).apply {
//                putExtra("detail_type", "20")//
        })
    }

    /**
     * 我的挂单
     */
    fun goZrMyGuaDan() {
        startActivity(Intent(this, MyGuaDan2Activity::class.java).apply {
//                putExtra("detail_type", "20")//
        })
    }

    //特价券 明细
    fun goLots1(num: String) {
//            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//                putExtra("detail_type", "20")//r_label还不确定
//            })
//        tGoodsDetail?.goods_dict?.let {
//        }
    }

    //优惠券 明细
    fun goLots2(num: String) {
//        tGoodsDetail?.goods_dict?.let {
////            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
////                putExtra("detail_type", "20")//r_label还不确定
////            })
//
//        }
    }


}